package com.zx.pay.wechat.controller;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.service.payments.jsapi.JsapiService;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/wechat/pay/jsapi")
public class JsapiPayController {

    @Autowired
    private Config config;

    @Value("${wechat.pay.app-id}")
    private String appId;

    @Value("${wechat.pay.merchant-id}")
    private String merchantId;

    @Value("${wechat.pay.notify-url}")
    private String notifyUrl;

    /**
     * JSAPI下单
     */
    @PostMapping("/prepay")
    public PrepayWithRequestPaymentResponse prepay(
            @RequestParam String outTradeNo,
            @RequestParam String description,
            @RequestParam int amount,
            @RequestParam String openid) {

        JsapiServiceExtension service = new JsapiServiceExtension.Builder().config(config).build();

        PrepayRequest request = new PrepayRequest();
        request.setAppid(appId);
        request.setMchid(merchantId);
        request.setNotifyUrl(notifyUrl);
        request.setDescription(description);
        request.setOutTradeNo(outTradeNo);

        Amount jsapiAmount = new Amount();
        jsapiAmount.setTotal(amount);
        request.setAmount(jsapiAmount);

        Payer payer = new Payer();
        payer.setOpenid(openid);
        request.setPayer(payer);

        return service.prepayWithRequestPayment(request);
    }

    /**
     * 查询订单（通过微信支付订单号）
     */
    @GetMapping("/order-by-id/{transactionId}")
    public com.wechat.pay.java.service.payments.model.Transaction queryOrderById(@PathVariable String transactionId) {
        JsapiService service = new JsapiService.Builder().config(config).build();

        QueryOrderByIdRequest request = new QueryOrderByIdRequest();
        request.setMchid(merchantId);
        request.setTransactionId(transactionId);

        return service.queryOrderById(request);
    }

    /**
     * 查询订单（通过商户订单号）
     */
    @GetMapping("/order-by-out-trade-no/{outTradeNo}")
    public com.wechat.pay.java.service.payments.model.Transaction queryOrderByOutTradeNo(@PathVariable String outTradeNo) {
        JsapiService service = new JsapiService.Builder().config(config).build();

        QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
        request.setMchid(merchantId);
        request.setOutTradeNo(outTradeNo);

        return service.queryOrderByOutTradeNo(request);
    }

    /**
     * 关闭订单
     */
    @PostMapping("/close-order/{outTradeNo}")
    public void closeOrder(@PathVariable String outTradeNo) {
        JsapiService service = new JsapiService.Builder().config(config).build();

        CloseOrderRequest request = new CloseOrderRequest();
        request.setMchid(merchantId);
        request.setOutTradeNo(outTradeNo);

        service.closeOrder(request);
    }
}